<?php

namespace App\Repositories\Eloquent;

use App\Models\PolicyUpload;
use App\Repositories\Contracts\PlatformInterface;
use App\Services\PolicyUpload\CtripService;
use App\Services\PolicyUpload\QnService;
use App\Services\PolicyUpload\RuleService;
use App\Services\PolicyUpload\TcService;
use Curl\CurlService\CurlService;
use Curl\StrService\StrService;
use MakeRep\Repository;

class PlatformRepository extends Repository implements PlatformInterface
{

    public $fillable = ['admin_id','policy_id','policy_code','airline','platform','flight_code','share_flight_code','depart_city_code','arrived_city_code','depart_at','cabin','price','seat','show','status','ext','upload_status','upload_msg'];


    function model()
    {
        return PolicyUpload::class;
    }


    /**
     *三大平台上传政策
     */
    public function platformUploadPolicy($param) {
//        dd($param);
        $msg = '';
        $hbCtripRes = false;
        $mhCtripRes = false;
        $ruleCtripRes = false;
        $qnRes = false;
        $tcRes = false;
        if (key_exists('hb', $param)) {
            $param['platform'] = 10;
            $hbCtripRes = $this->platformDataSet($param);

            if (is_string($hbCtripRes)) {
                $msg .= $hbCtripRes;
            } else if (is_bool($hbCtripRes) && !$hbCtripRes) {
                $msg .= '恒博携程失败,';
            }
        }
        if (key_exists('mh', $param)) {
            $param['platform'] = 11;
            $mhCtripRes = $this->platformDataSet($param);

            if (is_string($mhCtripRes)) {
                $msg .= $mhCtripRes;
            } else if (is_bool($mhCtripRes) && !$mhCtripRes) {
                $msg .= '茂华携程失败,';
            }
        }
        if (key_exists('gz', $param)) {
            $param['platform'] = 20;
            $ruleCtripRes = $this->platformDataSet($param);

            if (is_string($ruleCtripRes)) {
                $msg .= $ruleCtripRes;
            } else if (is_bool($ruleCtripRes) && !$ruleCtripRes) {
                $msg .= '规则运价携程失败,';
            }
        }
        if (key_exists('mhgz', $param)) {
            $param['platform'] = 21;
            $ruleCtripRes = $this->platformDataSet($param);

            if (is_string($ruleCtripRes)) {
                $msg .= $ruleCtripRes;
            } else if (is_bool($ruleCtripRes) && !$ruleCtripRes) {
                $msg .= '规则运价携程失败,';
            }
        }
        if (key_exists('qn', $param)) {
            $param['platform'] = 30;
            $qnRes = $this->platformDataSet($param);

            if (is_string($qnRes)) {
                $msg .= $qnRes;
            } else if (is_bool($qnRes) && !$qnRes) {
                $msg .= '去哪失败,';
            }
        }
        if (key_exists('tc', $param)) {
            $param['platform'] = 40;
            $tcRes = $this->platformDataSet($param);

            if (is_string($tcRes)) {
                $msg .= $tcRes;
            } else if (is_bool($tcRes) && !$tcRes) {
                $msg .= '同城失败';
            }
        }
        if ($msg) {
            return $msg;
        }
        return true;
    }

     /**
     * 三大平台 上传政策
     * @param $param
     * @return string|true|void
     */
    public function platformDataSet($param)
    {
        //平台上传

        switch ($param['platform']) {
            // 10=恒博携程，11=恒博携程
            case 10:
            case 11: {
                //处理数据
                $hbInfo = CtripService::platformUpload($param);

                $specialPolicyInterface = resolve('App\Repositories\Ota\Ctrip\Policy\Contracts\SpecialPolicyInterface');

                if ($param['platform'] == 10) {
                    $uploadRes = $specialPolicyInterface->add([$hbInfo]);
                } else {
                    $uploadRes = $specialPolicyInterface->addMh([$hbInfo]);
                }

                if ($uploadRes && isset($uploadRes['TradeResult'])) {
                    if (isset($uploadRes['TradeResult']['Result']) && isset($uploadRes['TradeResult']['Result']['SuccessList'])) {

                        //成功的话加入政策列表
                        $this->savePolicy($param, $uploadRes['TradeResult']['Result']['SuccessList'][0]);
                        return true;
                    } else {
                        return $uploadRes['TradeResult']['ErrorList'][0]['ErrorMessage'];
                    }
                }

                return false;
            }
            case 20:
            case 21:{

                //处理数据
                $hbInfo = RuleService::platformUpload($param);

                $specialPolicyInterface = resolve('App\Repositories\Ota\Ctrip\Policy\Contracts\CtripRulePolicyInterface');
                if ($param['platform'] == 20) {
                    $uploadRes = $specialPolicyInterface->add([$hbInfo]);
                } else {
                    $uploadRes = $specialPolicyInterface->addMh([$hbInfo]);
                }

            if ($uploadRes && isset($uploadRes['TradeResult'])) {
                if (isset($uploadRes['TradeResult']['Result']) && isset($uploadRes['TradeResult']['Result']['SuccessList'])) {

                    //成功的话加入政策列表
                    $hbInfo['platform'] = $param['platform'];
                    $hbInfo['admin'] = request()->admin;
                    $ruleInterface = resolve('App\Repositories\Contracts\RulePolicyInterface');
                    $ruleInterface->savePolicy($hbInfo, $uploadRes['TradeResult']['Result']['SuccessList'][0]);
                    return true;
                } else {
                    return $uploadRes['TradeResult']['ErrorList'][0]['ErrorMessage'];
                }
            }

            return false;

            }
            case 30: {

                QnService::QnDataSet($param);

                $param['ext']           = StrService::uuid();

                //先保存数据等待回调
                $res = $this->savePolicy($param, null);

                //处理数据
                $qnInfo = QnService::platformUpload($param);
                $qnInfo['url'] = $param['url'];
                $qnInfo['ext'] = $param['ext'];

                //处理数据
                $qnInfo['type']          = 'STOCK';
                $qnInfo['supportCba']    = '是';
                if (!empty($res)) {

                    $qnInfo['policycode'] .= '_'.$res['id'];
                }

//                dd($qnInfo);
                $specialPolicyInterface = resolve('App\Repositories\Ota\Qn\Policy\Contracts\QuPolicyInterface');
                $uploadRes = $specialPolicyInterface->add([$qnInfo]);

                if (!empty($uploadRes)){
                    $result = str_replace(["\n","\""], ["",""], $uploadRes);
                    if (empty($result)) {
                        return true;
                    } else {
                        return $result;
                    }
                }
                return false;
            }
            // 40=同程
            case 40: {

                //处理数据
                TcService::TcDataSet($param);

                //先保存数据等待回调
                $this->savePolicy($param, null);

                //处理数据
                $tcInfo = TcService::platformUpload($param);
                $tcInfo['url'] = $param['url'];
                $tcInfo['ext'] = $param['ext'];

                $specialPolicyInterface = resolve('App\Repositories\Ota\Tc\Policy\Contracts\TcPolicyInterface');

                $uploadRes = $specialPolicyInterface->addSpecial([$tcInfo]);

                if (!empty($uploadRes) && isset($uploadRes['success'])){
                    if ($uploadRes['success'] == true) {
                        return true;
                    } else if ($uploadRes['success'] == false) {
                        $errorMessage = $uploadRes['errorMessage'];
                        if($errorMessage) {
                            return $errorMessage;
                        }
                    }
                }
                return false;
            }
        }
    }


    /**
     * 三大平台 保存政策
     * @param $param
     * @param $policyId
     * @return void
     */
    private function savePolicy($param, $policyId){

        $data = [
            'admin_id'              => $param['admin']['id'],
            'policy_id'             => $policyId,
            'policy_code'           => $param['policy_code'],
            'airline'               => $param['hs_code'],
            'platform'              => $param['platform'],
            'flight_code'           => $param['flight_code'],
            'depart_city_code'      => $param['depart_city_code'],
            'arrived_city_code'     => $param['arrived_city_code'],
            'depart_at'             => $param['date'],
            'cabin'                 => $param['cabin_code'],
            'seat'                  => $param['seat'],
            'status'                => $param['status'] == 1 ? 1 : 2,
            'upload_status'         => 1,
        ];

        switch ($param['platform']) {
            // 10=恒博携程，11=恒博携程
            case 10:
            case 11: {
                $data['price']         = $param['set_price'];
                $data['upload_status'] = 2;
                break;
            }
            // 30=去哪
            case 30: {
                $data['price']         = $param['seller_price'];
                $data['ext']           = $param['ext'];
                $data['upload_status'] = 1;
                break;
            }
            // 40=同程
            case 40: {
                $data['price']         = $param['fix_price'];
                $data['ext']           = $param['ext'];
                $data['upload_status'] = 1;
                break;
            }
        }

        if (empty($param['id'])) {
            $res = $this->add($data);

            return $res;
        } else {
            $res = $this->update(['id'=>$param['id']],$data);
            return $param;
        }
    }

}